package com.leadership.entity.actionlearning;

import java.beans.Transient;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.springframework.format.annotation.DateTimeFormat;
import com.demo2do.core.utils.DateUtils;
import com.leadership.entity.user.User;

/**
 * 刻意行动计划表
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "action_learning")
public class ActionLearning {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /** 关联用户 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private User user;
    
    private Date createTime;
    
    /** 所属能力项 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Competency competency;
    
    /**
     * 刻意学习数量（至少选择一项）
     */
    private int anchors;
    
    private String challenge;
    
    @Enumerated(EnumType.STRING)
    private Status status;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    
    /** 关系助力 重要关系人*/
    private String importantRelation;
    
    /** 领导实践 刻意练习场景 */
    private String practiceScene;
    
    /** 当前项目 */
    private Long currentProjectId;
    
    /** 是否启用 */
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public ActionLearning() {
    }
    
    /**
     * 
     * @param anchors
     * @param user
     * @return
     */
    public ActionLearning initilization(int anchors, User user, Competency competency) {
        this.anchors = anchors;
        this.user = user;
        this.competency = competency;
        this.createTime = new Date();
        if(!startDate.after(createTime)) {
            this.status = Status.PROCESSING;
        } else {
            this.status = Status.CREATED;
        }
        this.currentProjectId = user.getCurrentProjectId();
        this.disabled = false;
        return this;
    }
    
    /**
     * 
     * @param actionLearning
     * @param anchorSize
     * @return
     */
    public ActionLearning edit(ActionLearning actionLearning, int anchorSize) {
        this.anchors = anchorSize;
        this.challenge = actionLearning.getChallenge();
        this.startDate = actionLearning.getStartDate();
        this.endDate = actionLearning.getEndDate();
        if(!startDate.after(createTime)) {
            this.status = Status.PROCESSING;
        } else {
            this.status = Status.CREATED;
        }
        this.importantRelation = actionLearning.getImportantRelation();
        this.practiceScene = actionLearning.getPracticeScene();
        return this;
    }
    
    /**
     * 
     * @param status
     * @return
     */
    public ActionLearning changeStatus(Status status) {
        this.status = status;
        return this;
    }
    
    @Transient
    public int getMonth() {
        int days = DateUtils.daysBetween(this.startDate, this.endDate);
        int month = days/30;
        return month;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the competency
     */
    public Competency getCompetency() {
        return competency;
    }

    /**
     * @return the anchors
     */
    public int getAnchors() {
        return anchors;
    }

    /**
     * @return the challenge
     */
    public String getChallenge() {
        return challenge;
    }

    /**
     * @return the status
     */
    public Status getStatus() {
        return status;
    }

    /**
     * @return the startDate
     */
    public Date getStartDate() {
        return startDate;
    }

    /**
     * @return the endDate
     */
    public Date getEndDate() {
        return endDate;
    }

    /**
     * @return the importantRelation
     */
    public String getImportantRelation() {
        return importantRelation;
    }

    /**
     * @return the practiceScene
     */
    public String getPracticeScene() {
        return practiceScene;
    }

    /**
     * @return the currentProjectId
     */
    public Long getCurrentProjectId() {
        return currentProjectId;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param competency the competency to set
     */
    public void setCompetency(Competency competency) {
        this.competency = competency;
    }

    /**
     * @param anchors the anchors to set
     */
    public void setAnchors(int anchors) {
        this.anchors = anchors;
    }

    /**
     * @param challenge the challenge to set
     */
    public void setChallenge(String challenge) {
        this.challenge = challenge;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(Status status) {
        this.status = status;
    }

    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    /**
     * @param endDate the endDate to set
     */
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    /**
     * @param importantRelation the importantRelation to set
     */
    public void setImportantRelation(String importantRelation) {
        this.importantRelation = importantRelation;
    }

    /**
     * @param practiceScene the practiceScene to set
     */
    public void setPracticeScene(String practiceScene) {
        this.practiceScene = practiceScene;
    }

    /**
     * @param currentProjectId the currentProjectId to set
     */
    public void setCurrentProjectId(Long currentProjectId) {
        this.currentProjectId = currentProjectId;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
